package com.collectplus.express.passport;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.CollectingPassportBean;
import com.collectplus.express.parcel.ParcelWaitTakeActivity;
import com.collectplus.express.parcel.ParcelWaitTakenActivity;
import com.collectplus.express.wxapi.WXshare;
import com.shouhuobao.bhi.collectpoint.ServicePointDetailsActivity;

import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * Created by lidandan on 2018/3/6.
 * passport代收
 */
public class NewPassportCollectingActivity extends BaseActivity2 implements View.OnClickListener {

    private CollectingPassportBean item;
    private LinearLayout passportExpressLayout;
    private NewPassportCollectingOperate collectingOperate;
    private NewPassportCollectingTakenOperate collectingTakenOperate;
    private TextView passportCollectingSeeParcel;
    private TextView passportOtherCollectingTV;

    // 顶部椭圆背景layout
    private ViewGroup mTopLayout;
    private View mMainViewLayout;

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 第二个方法在此参数无效
        overridePendingTransition(R.anim.in_bottom2top, 0);
        super.onCreate(savedInstanceState);
        Window win = getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        //        //         win.getDecorView().setMinimumWidth((int) (win.getWindowManager().getDefaultDisplay().getWidth() * 0.6));// 设置dialog的宽度
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        //        // lp.horizontalMargin = 300;
        lp.gravity = Gravity.CENTER;
        win.setAttributes(lp);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_new_passport_collecting);
        super.findViewById();
        collectingOperate = new NewPassportCollectingOperate(this, R.id.passport_route_item);
        collectingTakenOperate = new NewPassportCollectingTakenOperate(this, R.id.passport_taken_item);
        passportExpressLayout = (LinearLayout) findViewById(R.id.passport_collecting_express_layout);
        mMainViewLayout = findViewById(R.id.new_passport_main_ll);
        passportOtherCollectingTV = (TextView) findViewById(R.id.passport_other_collecting_parcel);
        passportOtherCollectingTV.setOnClickListener(this);
        final String stationgId = getIntent().getStringExtra("stationgId");
        final String takeId = getIntent().getStringExtra("takeId");
        item = (CollectingPassportBean) getIntent().getSerializableExtra("item");
        // 不是传过来的bean,调用接口获取数据
        if (item == null) {
            if (!Str.isEmpty(stationgId)) {
                showLoadingDialog("");
                AppHttp.getInstance().getPassportCollectingDetail(stationgId, takeId);
            } else {
                showToast("数据错误");
                finish();
            }
        } else {
            if (item != null) {
                updateData(item);
            }
        }
        findViewById(R.id.passport_collecting_ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //数据显示
    public void updateData(CollectingPassportBean item) {
        //顶部状态信息
        mTopLayout = (ViewGroup) findViewById(R.id.passport_collecting_top_layout);
        //代收驿站详情
        findViewById(R.id.passport_collecting_station_detail).setOnClickListener(this);
        //代收查看包裹
        passportCollectingSeeParcel = (TextView) findViewById(R.id.passport_collecting_see_parcel);
        passportCollectingSeeParcel.setOnClickListener(this);
        //处理顶部背景
        handleTopBackgroup(mTopLayout);
        //处理接收人信息
        handleReceiverInfo();
        //处理快递信息
        handleExpressInfo();

    }

    // 处理驿站信息
    private void handleReceiverInfo() {
        //顶部信息
        TextView label = (TextView) findViewById(R.id.passport_collecting_people_label);
        TextView receiverName = (TextView) findViewById(R.id.passport_collecting_people);
        label.setTextColor(getResources().getColor(R.color.app_fontcolor_333333));
        receiverName.setTextColor(getResources().getColor(R.color.app_fontcolor_333333));
        receiverName.setText(item.getStationName());
        if (item.getStatus() == 1) {
            mMainViewLayout.setVisibility(View.GONE);
            label.setText(item.getExpressCompanyName());
            passportExpressLayout.setVisibility(View.VISIBLE);
            collectingOperate.loadLayout(item);
            passportOtherCollectingTV.setText("让他人代取");
        } else {//if (item.getStatus() == 12||item.getStatus()==11)
            mMainViewLayout.setVisibility(View.GONE);
            label.setText(item.getTakeTime() + " 取件成功");
            passportExpressLayout.setVisibility(View.GONE);
            collectingTakenOperate.loadLayout(item);
            passportCollectingSeeParcel.setText("查看包裹详情");
            passportOtherCollectingTV.setText("分享领邮票");

        }
        //代收驿站信息
        TextView collectingStationNameTv = (TextView) findViewById(R.id.collecting_station_name_tv);
        TextView collectingStationTimeTv = (TextView) findViewById(R.id.collecting_station_time_tv);
        TextView collectingStationAddressTv = (TextView) findViewById(R.id.collecting_station_address_tv);
        collectingStationNameTv.setText("驿站：" + item.getStationName());
        collectingStationTimeTv.setText("营业时间：" + item.getServiceTimeStart() + "-" + item.getServiceTimeEnd());
        collectingStationAddressTv.setText("地址：" + item.getAddress());
        //驿站图片
        GlideUtil.loadImage(this, item.getHeadImage(), 0, (ImageView) findViewById(R.id.passport_collecting_station_image));
    }

    /**
     * 快递公司信息
     */
    private void handleExpressInfo() {
        TextView collectingExpressCodeTv = (TextView) findViewById(R.id.passport_collecting_express_code);
        ImageView collectingExpressImage = (ImageView) findViewById(R.id.passport_collecting_express_photo_img);
        TextView collectingExpressPhoneTv = (TextView) findViewById(R.id.passport_collecting_express_phone);
        if (isNotEmpty(item.getExpressNumber())) {
            collectingExpressCodeTv.setText(item.getExpressCompanyName() + ": " + item.getExpressNumber());
        } else {
            collectingExpressCodeTv.setText(item.getExpressCompanyName() + ": 暂无");
        }
        GlideUtil.loadImage(this, item.getExpressCompanyHeadImage(), 0, collectingExpressImage);
        collectingExpressPhoneTv.setText("收件手机号码：" + item.getUserPhone());
    }

    /**
     * 顶部背景
     *
     * @param view
     */
    public void handleTopBackgroup(View view) {
        ImageView logoIV = (ImageView) findViewById(R.id.passport_collecting_logo);
        int left = view.getPaddingLeft();
        int top = view.getPaddingTop();
        int right = view.getPaddingRight();
        int bottom = view.getPaddingBottom();
        logoIV.setImageResource(R.drawable.passport_top_logo_black_take);
        view.setBackgroundResource(R.drawable.passport_corners_top_3);
        view.setPadding(left, top, right, bottom);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //代收详情返回
            case MsgID.passport_collecting_detail:
                cancelLoadingDialog();
                final AppResult<CollectingPassportBean> parcelResult = JSON.parser(msg.obj, CollectingPassportBean.class);
                if (parcelResult.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                final CollectingPassportBean collectingDetail = parcelResult.getResult();
                                if (collectingDetail != null) {
                                    item = collectingDetail;
                                    updateData(collectingDetail);
                                } else {
                                    showToast("获取数据失败");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    showToast(parcelResult.getMessage());
                }
                return false;
            case MsgID.other_collecting_parcel:
                cancelLoadingDialog();
                final AppResult result = JSON.parser(msg.obj);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result.isSuccess()) {
                            String code = (String) result.getResult();
                            if (isNotEmpty(code)) {
                                DialogMgr.othersCollectingDialog((BaseActivity2) getContext(), code);
                            }
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            //驿站详情
            case R.id.passport_collecting_station_detail:
                intent.setClass(this, ServicePointDetailsActivity.class);
                intent.putExtra("stationId", item.getStationId());
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            //查看包裹或查看包裹详情  未取件显示查看包裹  取件成功显示查看包裹详情
            case R.id.passport_collecting_see_parcel:
                if (item.getStatus() == 1) {
                    intent.setClass(this, ParcelWaitTakeActivity.class);
                    intent.putExtra("item", item);
                    startActivity(intent);
                } else {
                    intent.setClass(this, ParcelWaitTakenActivity.class);
                    intent.putExtra("item", item);
                    startActivity(intent);
                }
                break;
            case R.id.passport_other_collecting_parcel:
                // 1未取件 12已取件
                //他人代收
                if (item.getStatus() == 1) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().otherCollectingParcel(item.getUserPhone(), item.getStationId());
                } //分享领邮票
                 else {
                    final WXshare mWXshare = new WXshare(this);
                    final String url = AppConfig.getServerUrl() + "static/collection/view/owner.html";
                    final String title = "送你几张邮票，以后用火箭投递寄快递吧！";
                    final String description = "";
                    final Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.stamp_share_img);
                    //微信好友
                    mWXshare.shareWX(0, title, description, icon, url, "gh_9899afdf21f8", "/pages/shareStamp/shareStamp?linkId=" + item.getId());
                }
                break;
        }
    }
    /**
     * 他人代收
     */
    public void otherCollectingShare() {
        final WXshare mWXshare = new WXshare(this);
        final String url = AppConfig.getServerUrl() + "static/collection/view/owner.html";
        final String title = "您的朋友邀请您帮忙取快递啦";
        final String description = "为了包裹安全，请勿将连接内数据泄漏给他人哦！";
        final Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.collecting_share_img);
        //微信好友
        mWXshare.shareWX(0, title, description, icon, url, "gh_9899afdf21f8", "/pages/passport/passport?stationId="
                + item.getStationId() + "&shareid=0&userid=" + AppCache.getUserId() + "&takeid=" + item.getTakeId());
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.out_top2bottom);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
